package com.edu.spring.ioc;

import com.edu.spring.ioc.bean.Car;
import com.edu.spring.ioc.bean.Person;
import com.edu.spring.ioc.bean.User;
import com.edu.spring.ioc.config.ImportConfig;
import com.edu.spring.ioc.controller.UserController;
import com.edu.spring.ioc.dao.DeliveryDao;
import com.edu.spring.ioc.datasource.MyDataSource;
import com.github.xiaoymin.knife4j.spring.util.EnvironmentUtils;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.ConfigurableApplicationContext;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Import;
import org.springframework.context.annotation.Lazy;

import java.util.Map;

/**
 * 这个是主入口类，称为主程序类
 * application.properties 就是这个项目的配置文件
 */
@SpringBootApplication
@Import(EnvironmentUtils.class)
public class Spring01IocApplication {

    public static void mainImport(String[] args) {
        ConfigurableApplicationContext ioc = SpringApplication.run(Spring01IocApplication.class, args);
        Map<String, EnvironmentUtils> beans = ioc.getBeansOfType(EnvironmentUtils.class);
        System.out.println("beans = " + beans);

        ImportConfig importConfig = new ImportConfig();
        System.out.println(importConfig.getEnvironmentUtils());
    }

    public static void main(String[] args) {
        // 1.跑起一个Spring的应用；ApplicationContext：Spring应用上下文对象；IoC容器
        ConfigurableApplicationContext ioc = SpringApplication.run(Spring01IocApplication.class, args);
//        System.out.println("IoC = " + ioc);

        // 2.容器中装了哪些组件
//        System.out.println("组件：");
//        Arrays.stream(ioc.getBeanDefinitionNames()).forEach(System.out::println);

        Map<String, Car> beansOfType = ioc.getBeansOfType(Car.class);
        System.out.println("beansOfType = " + beansOfType);

        Map<String, Person> personMap = ioc.getBeansOfType(Person.class);
        System.out.println(personMap);

        UserController userController = ioc.getBean(UserController.class);
        System.out.println(userController);
        System.out.println(userController.getPersonMap().get("Bill"));

        DeliveryDao bean = ioc.getBean(DeliveryDao.class);
        bean.saveDelivery();

        User user = ioc.getBean(User.class);
        System.out.println("运行：" + user);
        System.out.println("==========");

        System.out.println(ioc.getBean(MyDataSource.class));
    }

    // 3.给容器注册一个自己的组件；容器中的每个组件都有自己的名字，方法名就是组件的名字。
    @Bean
    public Person person() {
        Person person = new Person();
        person.setName("Tom");
        person.setAge(13);
        person.setGender("男");
        return person;
    }

    @Bean("Bill")
    @Lazy
    public Person bill() {
        Person person = new Person();
        person.setName("Bill");
        person.setAge(45);
        person.setGender("男");
        return person;
    }

    //    @Conditional(WindowsCondition.class)
    @Bean("Jobs")
    @ConditionalOnMissingBean(name = "Bill")
//    @ConditionalOnBean(name = "Bill")
    @Lazy
    public Person jobs() {
        Person person = new Person();
        person.setName("Jobs");
        person.setAge(35);
        person.setGender("男");
        return person;
    }

//    @Conditional(MacCondition.class)

}
